<template>
	<view class="login-container">
		<view class="u-demo-title">供应商系统登录</view>
		<u-input v-model="params.account" style="width: 100%;" placeholder="请输入账号" />
		<u-input type='password' v-model="params.password" style="width: 100%;" placeholder="请输入密码" />
		<u-button @click="toLogin" style="width: 100%; margin: 16px;" type='primary'>
			登录
		</u-button>
	</view>
</template>

<script>
	import { login } from '../../util/request.js'
	export default {
		data() {
			return {
				params: {
					account: '',
					password: ''
				}
			};
		},
		created() {
		},
		methods:{
			toLogin() {
				uni.showLoading({
					title: '正在登录'
				})
				login(this.params).then(res => {
					const { data, code, msg } = res.data
					if (code === 0) {
						uni.showToast({
							title: msg,
							icon: 'error'
						})
						return
					}
					// 登录成功后的处理
					const userinfo = data.userinfo
					const { token, user_id } = userinfo
					uni.setStorage({
						key: 'token',
						data: token
					})
					uni.setStorage({
						key: 'userinfo',
						data: userinfo
					})
					// account: "username"
					// createtime: 1634232242
					// expires_in: 2592000
					// expiretime: 1636824242
					// factory_ids: 109
					// id: 109
					// mobile: "17719496221"
					// name: "测试主场"
					// token: "ade77e75-9d05-40df-8369-0dd29e876f99"
					// user_id: 109
					uni.showToast({
						title: '登录成功！',
						icon:'success'
					})
					uni.redirectTo({
						url:'/pages/index/index'
					})
				}).catch(e => {
					uni.showToast({
						title: 登录失败,
						icon: 'error'
					})
				})
			}
		}
	}
</script>

<style lang="less">
.login-container{
	display: flex;
	align-items: center;
	justify-content: center;
	flex-direction: column;
	padding: 16px;
	box-sizing: border-box;
}
</style>
